package com.cooldatasoft.menu;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;

import com.cooldatasoft.web.cms.main.Main;
import com.cooldatasoft.web.cms.node.NodeListPage;
import com.cooldatasoft.web.cms.person.CreatePersonPage;
import com.cooldatasoft.web.cms.person.PersonListPage;
import com.cooldatasoft.web.cms.personProperty.PersonPropertyListPage;
import com.cooldatasoft.web.cms.user.UserListPage;
import com.cooldatasoft.web.cms.workExpProperty.WorkExpPropertyListPage;
/**
 * Class that generates the upper chrome menu and submenu items.
 * 
 * HOW TO USE:
 * 1. Create a list of LinkInfo objects for the Menu items
 * 2. Create a list of submenu LinkInfo objects for each menu.
 * 3. Create List<List<LinkInfo>> object for submenus
 * 4. Pass those lists to appropriate components, html code will be generated accordingly.
 * 
 * @author fatih mehmet ucar
 *
 */
public class MenuPanel extends Panel{

	private int numberOfMenu;
	List<List<LinkInfo>> submenuItemList;
	
	
	public MenuPanel(String id){
		super(id);
		
		LinkInfo topMenu1 = new LinkInfo(null,"Menu1");
		LinkInfo topMenu2 = new LinkInfo(Main.class,"Menu2");
		LinkInfo topMenu3 = new LinkInfo(Main.class,"MAIN3");
		LinkInfo topMenu4 = new LinkInfo(Main.class,"MAIN4");
		
		
		List<LinkInfo> topMenuLinkList=new ArrayList<LinkInfo>();
		topMenuLinkList.add(topMenu1);
		topMenuLinkList.add(topMenu2);
		topMenuLinkList.add(topMenu3);
		topMenuLinkList.add(topMenu4);
		
		
		LinkInfo menu1_1 = new LinkInfo(PersonListPage.class,"Person List");
		LinkInfo menu1_2 = new LinkInfo(CreatePersonPage.class,"Create New Person");
		LinkInfo menu1_3 = new LinkInfo(Main.class,"Menu1_3");
		
		LinkInfo menu2_1 = new LinkInfo(PersonPropertyListPage.class,"PersonProperty List");
		LinkInfo menu2_2 = new LinkInfo(WorkExpPropertyListPage.class,"Work Exp. Property List");
		LinkInfo menu2_3 = new LinkInfo(UserListPage.class,"User List");
		LinkInfo menu2_4 = new LinkInfo(NodeListPage.class,"Department List");
		
		LinkInfo menu3_1 = new LinkInfo(Main.class,"Menu3_1");
		LinkInfo menu4_1 = new LinkInfo(Main.class,"Menu4_1");
		LinkInfo menu4_2 = new LinkInfo(Main.class,"Menu4_2");
		LinkInfo menu4_3 = new LinkInfo(Main.class,"Menu4_3");
		LinkInfo menu4_4 = new LinkInfo(Main.class,"Menu4_4");
		
		
		List<LinkInfo> subMenu1=new ArrayList<LinkInfo>();
		List<LinkInfo> subMenu2=new ArrayList<LinkInfo>();
		List<LinkInfo> subMenu3=new ArrayList<LinkInfo>();
		List<LinkInfo> subMenu4=new ArrayList<LinkInfo>();
		
		subMenu1.add(menu1_1);
		subMenu1.add(menu1_2);
		subMenu1.add(menu1_3);
		
		subMenu2.add(menu2_1);
		subMenu2.add(menu2_2);
		subMenu2.add(menu2_3);
		subMenu2.add(menu2_4);
		
		subMenu3.add(menu3_1);
		
		subMenu4.add(menu4_1);
		subMenu4.add(menu4_2);
		subMenu4.add(menu4_3);
		subMenu4.add(menu4_4);
		
		submenuItemList = new ArrayList<List<LinkInfo>>();
		submenuItemList.add(subMenu1);
		submenuItemList.add(subMenu2);
		submenuItemList.add(subMenu3);
		submenuItemList.add(subMenu4);
		
		
		//********************************************************************
		
		ListView menuView = new ListView("menuLinkList",topMenuLinkList) {
			int itemCount = 0;
			public void populateItem(final ListItem item) {
				
				final LinkInfo linkInfo = (LinkInfo) item.getModelObject();
				Link link=new Link("menuLink"){
					@Override
					public void onClick() {
						if(linkInfo.getLinkPageClass()!=null){
							setResponsePage(linkInfo.getLinkPageClass());
						}
					}
				};
				
				//Adding submenu to menu item
				link.add(new AttributeModifier("rel",true,new Model("dropmenu"+itemCount)));
				numberOfMenu = itemCount++;
				
				
				Label linkText=new Label("linkText",linkInfo.getLinkText());
				linkText.setRenderBodyOnly(true);
				link.add(linkText);
				item.add(link);
			}			
		};		
		menuView.setReuseItems(true);
		add(menuView);	
		
		
		
		ListView submenuListView = new ListView("submenuList",submenuItemList) {
			int itemCount = 0;
			public void populateItem(final ListItem item) {
				
				WebMarkupContainer submenuDiv=new WebMarkupContainer("submenuDiv");
				submenuDiv.add(new AttributeModifier("id",true,new Model("dropmenu"+itemCount)));
				
				
				ListView submenuItem = new ListView("submenuItem",submenuItemList.get(itemCount)) {
					public void populateItem(final ListItem item) {						
						final LinkInfo linkInfo = (LinkInfo) item.getModelObject();
						Link link=new Link("menuLink"){
							@Override
							public void onClick() {
								setResponsePage(linkInfo.getLinkPageClass());
							}
						};
						
						Label linkText=new Label("linkText",linkInfo.getLinkText());
						linkText.setRenderBodyOnly(true);
						link.add(linkText);
						item.add(link);
						item.setRenderBodyOnly(true);
					}			
				};
				
				submenuDiv.add(submenuItem);
				itemCount++;
				item.add(submenuDiv);
				item.setRenderBodyOnly(true);
			}
		};
		submenuListView.setReuseItems(true);
		add(submenuListView);	
		setRenderBodyOnly(true);
	}
}
